#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'AirTestUtils.php';
require_once 'models/TestUser.php';

// init
AIR2_DBManager::init();
$browser = new AirHttpTest();
$browser->set_user("testuser", array());

// create a test user to look at
$u = new TestUser();
$u->save();
$uuid = $u->user_uuid;

// helper function to get a header value
function get_header_val($headers, $name) {
    $n = preg_match("/$name:.*/i", $headers, $matches);
    if ($n != 1) return '';
    return preg_replace("/^$name:\s*/i", '', $matches[0]);
}

plan(34);


/**********************
 * Verify the user page gives 200
 */
$browser->set_content_type(AirHttpTest::$HTML); // set to html
ok( $resp = $browser->http_get("/user/$uuid"), 'nav to userpage' );
is( $browser->resp_code(), 200, "userpage resp code" );


/**********************
 * Verify the photo url
 */
ok( $resp = $browser->http_get("/user/$uuid/photo"), 'nav to userphoto' );
is( $browser->resp_code(), 200, "userphoto resp code" );
$type = $browser->resp_content_type();
ok( preg_match('/image/', $type), "userphoto content type" );

// record cache-related headers
$headers = $browser->browser->getHeaders();
$etag = get_header_val($headers, 'ETag');
$clen = get_header_val($headers, 'Content-Length');
$lmod = get_header_val($headers, 'Last-Modified');


/**********************
 * Change to a non-existent image
 */
$d = json_encode(array('uuri_value' => $browser->base_url.'/css/img/fakefake.jpg'));
ok( $resp = $browser->http_post("/user/$uuid/photo.json", array('radix' => $d)), 'POST-badphoto' );
//diag( $resp );
is( $browser->resp_code(), 200, "POST-badphoto resp code" );
ok( $json = json_decode($resp, true), "JSON decode POST-badphoto" );
ok( $json['success'], "POST-badphoto success" );
$puuid = $json['radix']['uuri_uuid'];

// fetch the photo --- should be the same
ok( $resp = $browser->http_get("/user/$uuid/photo"), 'nav to badphoto' );
is( $browser->resp_code(), 200, "badphoto resp code" );
$headers = $browser->browser->getHeaders();
is( $type, $browser->resp_content_type(), 'badphoto content type' );
is( $etag, get_header_val($headers, 'ETag'), 'badphoto etag' );
is( $clen, get_header_val($headers, 'Content-Length'), 'badphoto content length' );
is( $lmod, get_header_val($headers, 'Last-Modified'), 'badphoto last modified' );


/**********************
 * Change to a non-image (the home page)
 */
$d = json_encode(array('uuri_value' => $browser->base_url));
ok( $resp = $browser->http_put("/user/$uuid/photo/$puuid.json", array('radix' => $d)), 'PUT-nonimage' );
is( $browser->resp_code(), 200, "PUT-nonimage resp code" );
ok( $json = json_decode($resp, true), "JSON decode PUT-nonimage" );
ok( $json['success'], "PUT-nonimage success" );

// fetch the photo --- should be the same
ok( $resp = $browser->http_get("/user/$uuid/photo"), 'nav to nonimage' );
is( $browser->resp_code(), 200, "nonimage resp code" );
$headers = $browser->browser->getHeaders();
is( $type, $browser->resp_content_type(), 'nonimage content type' );
is( $etag, get_header_val($headers, 'ETag'), 'nonimage etag' );
is( $clen, get_header_val($headers, 'Content-Length'), 'nonimage content length' );
is( $lmod, get_header_val($headers, 'Last-Modified'), 'nonimage last modified' );


/**********************
 * Change to an existing image
 */
$d = json_encode(array('uuri_value' => $browser->base_url.'/lib/extjs/resources/images/default/s.gif'));
ok( $resp = $browser->http_put("/user/$uuid/photo/$puuid.json", array('radix' => $d)), 'PUT-image' );
is( $browser->resp_code(), 200, "PUT-image resp code" );
ok( $json = json_decode($resp, true), "JSON decode PUT-image" );
ok( $json['success'], "PUT-image success" );

// fetch the photo --- should be different
ok( $resp = $browser->http_get("/user/$uuid/photo"), 'nav to image' );
is( $browser->resp_code(), 200, "image resp code" );
$headers = $browser->browser->getHeaders();
isnt( $etag, get_header_val($headers, 'ETag'), 'image etag' );
isnt( $clen, get_header_val($headers, 'Content-Length'), 'image content length' );
isnt( $lmod, get_header_val($headers, 'Last-Modified'), 'image last modified' );

